// pages/exam/practice/practice.js
const app = getApp();
const { api } = require('../../../api/base.js');
const { $Message } = require('../../../component/iView/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    first: ['单项选择'],
    second: ['新增'],
    third: ['图片', '视频'],
    subList: ['语文', ''],
    count: 0,
    caValue: '',
    nowCA:false,
    arrayList:[],
    selectInfo: '',
    flag:false,
    lastChild: null,
    form: {
      formIndex: 0,
      questionType: "1",
      style: "1",
      subjectIdList: []
    },
    btnActions:[{
      name:'上一步',
    },{
      name:'下一步',
    }],
    select: ['科目一','科目二','科目三','语文'],
    coverData: {},
    caList: ['科目一', '科目二', '科目三', '语文', '科目一', '科目二', '科目三', '语文', '科目三', '语文', '科目一', '科目二', '科目三', '语文'],
    qType:[
      {
        name: '单选题',
        questionType: 1,
      },
      {
        name: '多选题',
        questionType: 2,
      },
      {
        name: '判断题',
        questionType: 3,
      },
      {
        name: '填空题',
        questionType: 4,
      },
      {
        name: '简答题',
        questionType: 5,
      },
    ],
    buttonInfo:{
      pre:{
        name:'取消'
      },
      next:{
        name: '下一步'
      },
      count:0,
      selectInfo:[],
      haveChild: true,
      nowInfo: null,
      subjectList: [],
    },
    lastCategory: null,
    subjectList:[],
    questionId: null,
    formIndex: 0,
    random:1,
    answerFlag: false,
    an_flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  },
  showCover() {
    if(this.data.buttonInfo.count ===0) {
      this.getCategory();
    }
    this.showCategoryContent();
  },
  preTap() {
    const buttonInfo = this.data.buttonInfo;
    if (buttonInfo.count === 0) {
      this.hideCategoryContent();
      return;
    }
    buttonInfo.selectInfo.splice(buttonInfo.count,1);
    buttonInfo.count = buttonInfo.count - 1;
    buttonInfo.nowInfo = buttonInfo.selectInfo[buttonInfo.count];
    if (buttonInfo.count > 0) {
      buttonInfo.pre.name = '上一步'
      buttonInfo.next.name = '下一步'
    }else {
      buttonInfo.pre.name = '取消'
    }
    this.getCategory(buttonInfo.selectInfo[buttonInfo.count-1]);
    this.setData({
      buttonInfo,
    })
  },
  nextTap() {
    const buttonInfo = this.data.buttonInfo;
    if (buttonInfo.count === 0) {
      buttonInfo.pre.name = '上一步';
    }
    buttonInfo.count += 1;
    this.setData({
      buttonInfo,
    })
    if (buttonInfo.hasChild) {
      this.getCategory(buttonInfo.nowInfo);
    }else {
      this.hideCategoryContent();
      const lastCategory = buttonInfo.selectInfo[buttonInfo.selectInfo.length - 1];
      this.getSubject(lastCategory);
      this.setData({
        lastCategory,
        buttonInfo: {
          pre: {
            name: '取消'
          },
          next: {
            name: '下一步'
          },
          count: 0,
          selectInfo: [],
          haveChild: true,
          nowInfo: null,
          subjectList: [],
        },
      })
    }
  },
  getCategory(info) {
    let data = {};
    if(info) {
      data = { parentId: info.id };
    }
    api('/api/exam/category/list', 'GET', data).then(resp => {
       if(resp.result) {
         this.setData({
           arrayList: resp.data
         })
       }else {
         wx.showToast({
           title: '网络错误',
         })
       }
    }).catch(err => {
      throw new Error(err);
    })
  },
  getNextCategory(e) {
    const ids = e.currentTarget.dataset.ids;
    const info = this.data.arrayList[ids];
    const buttonInfo = this.data.buttonInfo;
    buttonInfo.selectInfo[buttonInfo.count] = info;
    buttonInfo.nowInfo = info;
    if (info.hasChild) {
      buttonInfo.hasChild = true;
      buttonInfo.next.name = '下一步';
    } else {
      buttonInfo.hasChild = false;
      buttonInfo.next.name = '确认';
    }
    this.setData({
      buttonInfo,
    })
  },
  getSubject(info) {
    const data = { categoryId: info.id };
    api('/api/exam/subject/list', 'GET', data).then(resp => {
      if (resp.result) {
        this.setData({
          subjectList: resp.data.records
        })
      } else {
        wx.showToast({
          title: '网络错误',
        })
      }
    }).catch(err => {
      throw new Error(err);
    })
  },
  getExercise() {
    const id = this.data.subjectList[this.data.sIndex].id;
    const { qIndex, random, formIndex, qType } = this.data;
    
    const data= {
      formIndex: formIndex,
      questionType: qType[qIndex]['questionType'],
      style: random,
      subjectIdList: [id]
    };
    api('/api/exam/question/saveExerciseQuestion', 'POST', data).then(resp => {
      if (resp.result) {
        this.setData({
          questionId: resp.data
        })
        this.getQuestionList({ exerciseKey:resp.data});
      } else {
        wx.showToast({
          title: '网络错误',
        })
      }
    }).catch(err => {
      throw new Error(err);
    })
  },
  getQuestionList(data) {
    api('/api/exam/paper/exercise', 'GET', data).then(resp => {
      if (resp.result) {
        this.setData({
          questionList: resp.data.questionItems,
          answerFlag: true,
        })
      } else {
        wx.showToast({
          title: '网络错误',
        })
      }
    }).catch(err => {
      throw new Error(err);
    })
  },
  submitPractice(e) {
    console.log(e);
    const value = e.datail.value;
    this.data.form.questionType = value.questionType;
    this.data.form.style = value.style;
    this.data.form.subjectIdList = [this.subInfo.id];
   //  this.getExercise(this.data.form);
  },
  getPapers() {
    this.getExercise()
  },
  showAnswers() {
    this.setData({
      an_flag: false
    })
  },
  bindSubjectChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      sIndex: e.detail.value
    })

    // this.getExercise();
  },
  bindQuestionTypeChange(e) {
    this.setData({
      qIndex: e.detail.value
    })
  },
  getQuestionRandom(e) {
    const id = e.detail.value;
    this.setData({
      random:id,
    })
  },
  getQuestionPosition(e) {
    const value =e.detail.value;
    this.setData({
      formIndex: value
    })
  },
  showCategoryContent() {
    let animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease',
    })
    animation.opacity(1).step();
    this.setData({
      coverData: animation.export(),
      flag: true,
    })
    setTimeout(function () {
      animation.translateY(0).step();
      this.setData({
        coverData: animation.export()
      })
    }.bind(this), 400)
  },
  hideCategoryContent() {
    let animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'ease',
    })
    animation.opacity(1).step();
    this.setData({
      coverData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(250).step();
      this.setData({
        coverData: animation.export(),
        flag: false
      })
    }.bind(this), 400)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})